﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AutoF3
{
    public partial class MainForm : Form
    {
        List<ProblemRetriever> pr;
        SheetBuilder sb;

        public MainForm(List<ProblemRetriever> pr, SheetBuilder sb)
        {
            InitializeComponent();
            this.pr = pr;
            this.sb = sb;
        }

        private void selectProblemSheetFilename_Click(object sender, EventArgs e)
        {
            problemsSheetSaveFileDialog.Filter = "TeX Source Files (*.tex)|*.tex|All Files (*.*)|*.*";
            problemsSheetSaveFileDialog.FilterIndex = 0;

            if (problemsSheetSaveFileDialog.ShowDialog() == DialogResult.OK)
                problemSheetFileNameTextBox.Text = problemsSheetSaveFileDialog.FileName;
        }

        private void selectSolutionsSheetFilename_Click(object sender, EventArgs e)
        {
            solutionsSheetSaveFileDialog.Filter = "TeX Source Files (*.tex)|*.tex|All Files (*.*)|*.*";
            solutionsSheetSaveFileDialog.FilterIndex = 0;

            if (solutionsSheetSaveFileDialog.ShowDialog() == DialogResult.OK)
                solutionsSheetFileNameTextBox.Text = solutionsSheetSaveFileDialog.FileName;
        }

        private void addProblemButton_Click(object sender, EventArgs e)
        {
            var addProb = new ProblemTypeForm(pr, sb);
            addProb.ShowDialog();
            RefreshListbox();
        }

        private void createButton_Click(object sender, EventArgs e)
        {
            string problemSheetFileName = problemSheetFileNameTextBox.Text;
            string solutionsSheetFileName = solutionsSheetFileNameTextBox.Text;

            if (!checkBox1.Checked)
                solutionsSheetFileName = null;

            var buildSheet = new BuildSheetForm(sb, problemSheetFileName, solutionsSheetFileName);
            buildSheet.ShowDialog();
        }

        private void RefreshListbox()
        {
            problemListBox.Items.Clear();
            foreach (var name in sb.problemNameList)
                problemListBox.Items.Add(name);
        }

        private void editProblemButton_Click(object sender, EventArgs e)
        {
            int index = problemListBox.SelectedIndex;

            if (index >= 0)
            {
                var problem = new List<Problem>();
                problem.Add(sb.problemList[index]);

                var editProblem = new AddProblemForm(problem, sb, index);
                editProblem.ShowDialog();
                RefreshListbox();
            }
        }

        private void deleteProblemButton_Click(object sender, EventArgs e)
        {
            int index = problemListBox.SelectedIndex;

            if (index >= 0)
            {
                sb.DeleteAtIndex(index);
                RefreshListbox();
            }
        }

        private void problemListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = problemListBox.SelectedIndex;

            if (index >= 0)
            {
                editProblemButton.Enabled = true;
                deleteProblemButton.Enabled = true;
            }
            else 
            {
                editProblemButton.Enabled = false;
                deleteProblemButton.Enabled = false;
            }
        }

        private void problemSheetFileNameTextBox_TextChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(problemSheetFileNameTextBox.Text))
                createButton.Enabled = true;
            else
                createButton.Enabled = false;
        }
    }

    public class SelectedTextInfo
    {
        public int startIndex, length;

        public SelectedTextInfo(int startIndex, int length)
        {
            this.startIndex = startIndex;
            this.length = length;
        }
    }
}
